package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.hjq.widget.layout.SettingBar;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.LinkageBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.utils.LinkageDataHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * time   : 2021/12/22
 */
public final class HumanBodyInductionActivity extends AppActivity {

    private SettingBar settingReferenceArea;
    private SettingBar settingDataSourceDevice;
    private TextView tvCondition;
    private TextView tvSensitivityHint;
    private RadioGroup whenRadioGroup;
    // 1有人、0无人
    private int mValue = 1;
    private String deviceSerialNo;
    private String serialNo;
    private String areaName;
    private String areaCoding;
    private String deviceName;
    private LinkageBean mLinkageBean;
    private LinkageBean.LinkageCondition mLinkageCondition;
    private Integer sensitivity;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_human_body_induction;
    }

    @Override
    protected void initView() {
        settingReferenceArea = findViewById(R.id.setting_reference_area);
        tvSensitivityHint = findViewById(R.id.tv_sensitivity_hint);
        settingReferenceArea.setEnabled(false);
        settingDataSourceDevice = findViewById(R.id.setting_data_source_device);
        settingReferenceArea.setRightDrawable(null);
        settingDataSourceDevice.setVisibility(View.VISIBLE);
        setOnClickListener(settingDataSourceDevice);
        tvCondition = findViewById(R.id.tv_condition);
        whenRadioGroup = findViewById(R.id.when_radio_group);
        whenRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.type_somebody:
                        mValue = 1;
                        tvCondition.setText(getString(R.string.effective_when_time_holder, getString(R.string.somebody)));
                        if(sensitivity != null){
                            tvSensitivityHint.setText(getString(R.string.unmanned_delay_sensitivity_hint, getString(R.string.somebody), sensitivity));
                        }
                        break;
                    case R.id.type_nobody:
                        mValue = 0;
                        tvCondition.setText(getString(R.string.effective_when_time_holder, getString(R.string.nobody)));
                        if(sensitivity != null){
                            tvSensitivityHint.setText(getString(R.string.unmanned_delay_sensitivity_hint, getString(R.string.nobody), sensitivity));
                        }
                        break;
                }
            }
        });
        ((RadioButton)whenRadioGroup.getChildAt(0)).setChecked(true);
    }

    @Override
    protected void initData() {
        serialNo = getString("serial_no");
        deviceSerialNo = getString("device_serial_no");
        areaCoding = getString("room_serial_no");
        areaName = getString("floor_room_name");
        deviceName = getString("device_name");
        mLinkageBean = LinkageDataHolder.getLinkageData();
        if (mLinkageBean != null) {
            List<LinkageBean.LinkageCondition> linkageConditions = mLinkageBean.getLinkageConditions();
            if (linkageConditions != null && linkageConditions.size() > 0 && !TextUtils.isEmpty(serialNo)) {
                for (LinkageBean.LinkageCondition item : linkageConditions) {
                    if (item.getConditionSerialNo().equals(serialNo)) {
                        mLinkageCondition = item;
                        deviceSerialNo = mLinkageCondition.getDeviceSerialNo();
                        areaName = mLinkageCondition.getAreaName();
                        areaCoding = mLinkageCondition.getAreaCoding();
                        try {
                            mValue = Integer.valueOf(mLinkageCondition.getStateValue());
                        }catch (Exception e){

                        }
                        break;
                    }
                }
            }
        }
        DeviceBean device = LLDeviceDBManager.getInstance(HumanBodyInductionActivity.this).get(deviceSerialNo);
        if(device != null){
            deviceName = device.getName();
            setSensitivityHint(device);
        }
        updateView();
    }

    private void updateView() {
        List<DeviceBean> data = LLDeviceDBManager.getInstance(getContext()).getRoomEnvironmentDevices(areaCoding, DeviceBean.ENVIRONMENT_TYPE_HUMAN_BODY_INDUCTION);
        if(data.size() > 1){
            settingDataSourceDevice.setRightDrawable(AppApplication.getInstance().getThemeDrawable(R.attr.icon_list_next_normal));
        }else{
            settingDataSourceDevice.setRightDrawable(null);
        }
        settingReferenceArea.setRightText(areaName);
        settingDataSourceDevice.setRightText(deviceName);
        switch (mValue) {
            case 1:
                ((RadioButton) whenRadioGroup.getChildAt(0)).setChecked(true);
                break;
            case 0:
                ((RadioButton) whenRadioGroup.getChildAt(2)).setChecked(true);
                break;
        }
    }

    private void setSensitivityHint(DeviceBean device){
        sensitivity = device.getSensitivity();
        if(sensitivity != null){
            if(mValue == 1){
                tvSensitivityHint.setText(getString(R.string.unmanned_delay_sensitivity_hint, getString(R.string.somebody), sensitivity));
            }else{
                tvSensitivityHint.setText(getString(R.string.unmanned_delay_sensitivity_hint, getString(R.string.nobody), sensitivity));
            }
        }
    }

    @Override
    public void onRightClick(View view) {
        //当前联动空，才去
        if (mLinkageCondition == null) {
            //本地保存联动条件
            if (mLinkageBean == null) {
                mLinkageBean = new LinkageBean();
                LinkageDataHolder.setLinkageData(mLinkageBean);
            }
            List<LinkageBean.LinkageCondition> linkageConditions = mLinkageBean.getLinkageConditions();
            if (linkageConditions == null) {
                linkageConditions = new ArrayList<>();
            }
            mLinkageBean.setLinkageConditions(linkageConditions);
            mLinkageCondition = new LinkageBean.LinkageCondition();
            //先设置个本地的SerialNo，为了编辑找到对应的数据
            mLinkageCondition.setConditionSerialNo(UUID.randomUUID().toString());
            mLinkageCondition.setConditionType(9);
            linkageConditions.add(mLinkageCondition);
            mLinkageCondition.setSort(linkageConditions.indexOf(mLinkageCondition) + 1);
        }
        mLinkageCondition.setAreaType(1);
        mLinkageCondition.setDeviceSerialNo(deviceSerialNo);
        mLinkageCondition.setAreaName(areaName);
        mLinkageCondition.setAreaCoding(areaCoding);
        mLinkageCondition.setBeginValue(mValue);
        mLinkageCondition.setEndValue(mValue);
        mLinkageCondition.setLogicType(2);
        mLinkageCondition.setImageUrl("icon_intelligent_environment_pir");
        mLinkageCondition.setBriefing("人体感应");
        mLinkageCondition.setStateName("trigger");
        mLinkageCondition.setStateValue(mValue + "");
        startActivity_SingleTask(EditLinkageActivity.class);
        finish();
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.setting_data_source_device:
                intent = new Intent(this, DataSourceDeviceActivity.class);
                intent.putExtra("device_serial_no", deviceSerialNo);
                intent.putExtra("room_serial_no", areaCoding);
                intent.putExtra("environment_type", DeviceBean.ENVIRONMENT_TYPE_HUMAN_BODY_INDUCTION);
                startActivityForResult(intent, new OnActivityCallback() {
                    @Override
                    public void onActivityResult(int resultCode, @Nullable Intent data) {
                        if(resultCode == RESULT_OK){
                            areaName = data.getStringExtra("floor_room_name");
                            areaCoding = data.getStringExtra("room_serial_no");
                            deviceName = data.getStringExtra("device_name");
                            deviceSerialNo = data.getStringExtra("device_serial_no");
                            settingReferenceArea.setRightText(areaName);
                            settingDataSourceDevice.setRightText(deviceName);
                            DeviceBean device = LLDeviceDBManager.getInstance(HumanBodyInductionActivity.this).get(deviceSerialNo);
                            if(device != null){
                                setSensitivityHint(device);
                            }
                        }
                    }
                });
                break;
        }
    }
}